Repository navigation
Conversation
|
Visit the preview URL for this PR (updated for commit 1a9fd92): https://yew-rs-api--pr4219-master-im3mvlbo.web.app (expires Sat, 14 Nov 2026 11:51:35 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 |
Size ComparisonDetails
✅ None of the examples has changed their size significantly. |
603c1eb to
80cd486
Compare
|
@WorldSEnder Modified. Please review it again. |
There was a problem hiding this comment.
Took a bit, since I am myself not familiar with this part of the library and was hoping Mado would chime in. So bear with and feel free to correct me too.
completed being an (unbounded) hashmap is I believe a memory leak (of all failed requests that never succeed). Keeping the old property of the LRU cache seems worthwhile.
I would phrase the property we want as follows: Every use of the use_linked_state hook has associated with it an input, call this the query. So when the input changes, the query does but it's the same hook. Every query should lead to at most one request to the backend (this is what I understand the bug is about). Multiple simultaneous queries from different uses of the hook for the same input should share the request (only one at best) - implemented by in_flight and refreshing. A query we already answered in SSR should lead to no request (subtly a different hook might have the same query without SSR, this can lead to a query request since cache is unreliable). And finally, memory should be proportional to the hooks used. After unmounting all, only a constant should be left, that's why LRU cache is used but HashMap is used only for things in flight.
The last property I think implies that two different hooks querying for the same input might not always share the result (which could have dropped out of the LRU already) and the hook needs to have a local cache, too.
EDIT: To come back to my earlier comment: each component should remember if a request failed and not retry again. The shared cache should be used only for successful requests.
Sorry this took so long, caching is hard.
|
Note: prior art for request coalescing in rs-singleflight could be helpful. |
|
@WorldSEnder Thanks for spelling out the query lifetime and for the rs-singleflight pointer. I removed the shared completed map. Each hook now keeps the result for its current input, including failures, and the bounded LRU stores only successful values. Initial fetches and explicit refreshes share one in-flight request per key. Hooks that join a request can still read its result after it leaves the in-flight map, even if the LRU entry has been evicted. SSR-prepared results are used directly, and an internal refresh error keeps the previous value visible. I added a browser regression test for request coalescing, explicit retries, and LRU eviction. The updated PR is squashed into one commit and ready for another look. |
Madoshakalaka
left a comment
There was a problem hiding this comment.
Thanks for sticking with this through several rounds, and sorry for the long silence on my side.
The fix itself is right. I merged the branch onto current master locally: the new browser test passes, the same test fails against master's implementation (it times out waiting for the error state), and clippy on Rust 1.100 is clean for host and wasm32. Storing Rc<T> in the LRU instead of JSON is also a nice improvement.
One thing I'd like changed before merging.
Hooks sharing a key no longer see each other's refresh
I checked this with a variation of your test: two Watch components use the same input and both show the initial error. Clicking only the first one refreshes it to 10, but the second still shows error, even after it re-renders, although the shared cache now holds the good value.
The same applies to successful values: after A refreshes, B keeps its old value until it remounts, changes input, or refreshes itself. On master, a non-hydrated B reads the shared cache on every render and picks the new value up. I understand this is deliberate (the docs now say a background refresh updates the hooks that requested it), but for a data-fetching hook I think components showing the same (T, Input) should converge.
A small way to get there: on each render where the hook has no pending request, peek the shared cache (peek, so it doesn't affect LRU order) and adopt the cached value if it is a different Rc from the hook's own result. That restores master's behaviour and doesn't bring the retry loop back, since failures still live only in the hook. If the entry has been evicted, the hook keeps its local result as it does now. Please add the two-component case to the test.
Smaller points, not blocking
refresh()now joins any in-flight request for the key, including one that started before the refresh was requested. A "mutate, then refresh" flow can get pre-mutation data in that window; master always sent a fresh request. Either let refresh join only other refreshes, or document the behaviour onrefresh.- The
shrink_to_fitbookkeeping on the in-flight map isn't needed; the map only ever holds requests that are in flight.
The branch also needs updating onto master to pick up the Rust 1.100 CI fix from #4248.
Description
Fix the client-side
use_linked_stateretry loop caused by initialLinkError::Internalresults not being cached.Initial fetches now cache all completed results, including infrastructure errors. This allows the suspension to resolve and exposes the error through
LinkedStateHandle::as_result()instead of creating another identical request.Refresh behavior remains unchanged:
LinkError::Internalresults from background refreshes are not cached, so an existing stale value is preserved.Fixes #4218
Checklist